package action

import (
	"core/corestruct"
	"core/coreutil"
	"core/myerrors"
	"ht/api/htlogin/service"
	"ht/htinfo"
	"reflect"
	"time"
)

const (
	YZMPrev = "YZM"
)

type CaptchaAction struct {
}

func (p *CaptchaAction) Do(ctx *corestruct.HttpContext) error {
	var c = ctx.Http
	var code = c.Query("id")
	if "" == code {
		return myerrors.New("验证码为空")
	}
	var capid = service.CapthcaService.CreateImage()
	coreutil.RedisUtil.Set(YZMPrev+code, capid, time.Minute*5)

	var img = service.CapthcaService.GetImageByte(capid)
	if img == nil {
		return myerrors.New("验证码错误")
	}
	c.Header("Content-Type", "image/png")
	c.Writer.Write(img)
	c.AbortWithStatus(200)
	return nil
}

func CapthcaActionInit() {
	service.CapthcaServiceInit()

	var mact = &CaptchaAction{}
	var maction = corestruct.Action{
		Method: "get",

		FullUrl: "/htlogin/captcha",
		Name:    "获取验证码图片",
		Ename:   "",
		Memo:    "",

		PinType:    "",
		PinIslist:  false,
		PoutType:   "",
		PoutIslist: false,
		PoutIspage: false,

		AuthType: corestruct.AuthType_NONE,

		LogNeed:     false,
		LogPin:      false,
		LogPout:     false,
		LogTemplete: "",

		PrjName: htinfo.PRJ_NAME,
		Value:   reflect.ValueOf(mact),
	}
	coreutil.ActionUtil.Add(maction)

}
